package com.atguigu.Controller;

import com.atguigu.Serivce.AddressBookService;
import com.atguigu.Utils.BaseContext;
import com.atguigu.Utils.R;
import com.atguigu.pojo.AddressBook;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R addAddressBook(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setCreateUser(BaseContext.getCurrentId());
        addressBook.setUpdateUser(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    @PutMapping("default")//设置为默认地址
    public R updateDefault(@RequestBody AddressBook addressBook) {
        //先设置目标用户所有的地址都不为默认
        LambdaQueryWrapper<AddressBook> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookService.update(addressBook,queryWrapper);

        //再设置目标地址为默认
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    @GetMapping("/{id}")//根据id查询地址，回显
    public R getAddressBookById(@PathVariable("id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook == null) {
            return R.error("没有找到该对象");
        }
        return R.success(addressBook);
    }

    @GetMapping("default")//查询默认地址
    public R getDefaultAddressBook() {
        LambdaQueryWrapper<AddressBook> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getIsDefault,1);
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());

        AddressBook one = addressBookService.getOne(queryWrapper);
        if (one == null) {
            return R.error("没有该对象");
        }
        return R.success(one);
    }

    @GetMapping("/list")//查询用户所有的地址信息
    public R getAddressBookList(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<AddressBook> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(),AddressBook::getUserId,BaseContext.getThreadLocal());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

    @PutMapping
    public R updateAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R deleteAddressBook(Long ids) {
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }
}
